package com.we823.cms.controller.manage;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.Direction;
import com.we823.cms.common.web.pageable.PageRequest;
import com.we823.cms.common.web.pageable.Sort;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.cms.common.json.model.JqgridModel;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.web.Oper;
import com.we823.cms.core.security.shiro.PermissionStatus;
import com.we823.cms.model.BaseModel;
import com.we823.cms.repository.service.BaseService;

public abstract class AbstractSimpleManageController<T extends BaseModel<ID>,ID extends Serializable> extends SecurityBaseController {
	protected String baseUrl = "";
	private BaseService<T, ID> baseService;

	public AbstractSimpleManageController(BaseService<T, ID> baseService) {
		this.baseService = baseService;
		setIndetity();
	}
	
	@RequestMapping("list")
	public String list(){
		check(PermissionStatus.VIEW);
		return baseUrl + "list";
	}
	
	@RequestMapping("getJson")
	@ResponseBody
	public Model getJson(HttpServletRequest request,Model model){
		check(PermissionStatus.VIEW);
		boolean pageable = RequestUtil.getPageable(request);
		JqgridModel<T> jqgridModel = new JqgridModel<>(request, model);
		PageRequest pageRequest = getPageRequest(jqgridModel, request);
		
		PageInfo<T> dataPage = getData(pageRequest, request, model, pageable);
		
		if(dataPage!=null){
			jqgridModel.setDatarows(dataPage.getList());
			jqgridModel.setRecords(dataPage.getTotal());
		}
		jqgridModel.flush();
		return model;
	}
	
	protected PageRequest getPageRequest(JqgridModel<T> jqgridModel,HttpServletRequest request){
		PageRequest pageRequest = null;
		int page = jqgridModel.getPage()-1;
		int size = jqgridModel.getRows();
		Sort sort = getSort(request);
		if(sort!=null){
			pageRequest = new PageRequest(page, size, sort);
		}else{
			pageRequest = new PageRequest(page, size);
		}
		return pageRequest;
	}
	
	/**
	 * 默认排序方式为jqgrid的指定的排序方式，若不使用默认排序，则覆写此方法
	 * @param request
	 * @return
	 */
	public Sort getSort(HttpServletRequest request){
		String sortString = RequestUtil.getValueByName(request, "sidx","");
		String sortType = RequestUtil.getValueByName(request, "sord", "asc");
		if(!"".equals(sortString)){
			Direction direction = Direction.parse(sortType);
			Sort sort = new Sort(direction, sortString);
			return sort;
		}else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("process")
	@ResponseBody
	public Model process(HttpServletRequest request,Model model){
		String oper = RequestUtil.getValueByName(request, "oper","");
		switch (oper) {
		case "add":{
			check(PermissionStatus.CREATE);
			T entity = getEntity(request,null,Oper.ADD);
			baseService.add(entity);
			break;
		}
		case "edit":{
			check(PermissionStatus.UPDATE);
			Class<Integer> classname = Integer.class;
			ID id = (ID) RequestUtil.getValueByName(request, "id",classname);
			T entity = getEntity(request,id,Oper.EDIT);
			baseService.edit(entity,id);
			break;
		}
		case "del":{
			check(PermissionStatus.DELETE);
			ID id = (ID) RequestUtil.getValueByName(request, "id", 0);
			if(id != null){
				baseService.delete(id);			
			}
			break;
		}
		default:
			break;
		}
		return model;
	}

	/**
	 * 
	 * @param pageRequest
	 * @param request
	 * @param model
	 * @param pageable
	 * @return
	 */
	public abstract PageInfo<T> getData(PageRequest pageRequest,HttpServletRequest request,Model model,boolean pageable);
	/**
	 * 根据request获取Entity，当ID不为null时，需要赋值给ID
	 * @param request
	 * @param id
	 * @return
	 */
	public abstract T getEntity(HttpServletRequest request,ID id,Oper oper);
}
